package team.knowers.qzh.qzhserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhserver.common.convention.result.Results;
import team.knowers.qzh.qzhserver.common.database.BaseDO;
import team.knowers.qzh.qzhserver.dao.entity.CourseDO;
import team.knowers.qzh.qzhserver.dao.entity.CourseEvaluationDO;
import team.knowers.qzh.qzhserver.dao.entity.UserDO;
import team.knowers.qzh.qzhserver.dao.mapper.CourseMapper;
import team.knowers.qzh.qzhserver.dto.resp.CourseDetailsRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.CourseSimpleRespDTO;
import team.knowers.qzh.qzhserver.service.CourseEvaluationService;
import team.knowers.qzh.qzhserver.service.CourseService;
import team.knowers.qzh.qzhserver.service.UserService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程服务实现层
 *
 * @author xtg
 */
@Service
@RequiredArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseDO> implements CourseService {
    private final UserService userService;
    private final CourseEvaluationService courseEvaluationService;

    @Override
    public CourseDetailsRespDTO getCourseDetailsById(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getId, id).eq(CourseDO::getDelFlag, 0);
        CourseDO one = baseMapper.selectOne(queryWrapper);
        CourseDetailsRespDTO result = BeanUtil.toBean(one, CourseDetailsRespDTO.class);
        Long userId = result.getUserId();
        UserDO userDO = userService.getBaseMapper().selectOne(Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getId, userId));
        result.setAuthorName(userDO.getNickname());
        result.setAuthorAvatar(userDO.getAvatar());
        return result;
    }

    @Override
    public String getMarkById(Long id) {
        LambdaQueryWrapper<CourseEvaluationDO> queryWrapper = Wrappers.lambdaQuery(CourseEvaluationDO.class).eq(CourseEvaluationDO::getCourseId, id).eq(CourseEvaluationDO::getDelFlag, 0);
        List<CourseEvaluationDO> courseEvaluationDOS = courseEvaluationService.getBaseMapper().selectList(queryWrapper);
        double mark = 0;
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (CourseEvaluationDO each : courseEvaluationDOS) {
            mark += each.getMark();
        }
        if (courseEvaluationDOS.isEmpty()) {
            return "0.0";
        } else {
            mark /= courseEvaluationDOS.size();
            return decimalFormat.format(mark);
        }
    }

    @Override
    public Boolean addSalesVolume(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId, id).eq(CourseDO::getDelFlag, 0)
                .eq(CourseDO::getStatus, 2);
        CourseDO one = baseMapper.selectOne(queryWrapper);
        one.setSalesVolume(one.getSalesVolume() + 1);
        LambdaUpdateWrapper<CourseDO> updateWrapper = Wrappers.lambdaUpdate(CourseDO.class).eq(CourseDO::getId, id).eq(CourseDO::getDelFlag, 0).eq(CourseDO::getStatus, 2);
        baseMapper.update(one, updateWrapper);
        return true;
    }

    @Override
    public List<CourseSimpleRespDTO> homeUserCourseByTimeDesc(Long userId) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getDelFlag, 0).eq(CourseDO::getUserId, userId).orderByDesc(BaseDO::getCreateTime);
        List<CourseDO> courseDOS = baseMapper.selectList(queryWrapper);
        List<CourseSimpleRespDTO> results = new ArrayList<>();
        for (CourseDO each : courseDOS) {
            results.add(BeanUtil.toBean(each, CourseSimpleRespDTO.class));
        }
        return results;
    }

}
